package cn.growthgate.fgo.web;

import java.util.Date;

import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresRoles;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.plugins.Page;

import cn.growthgate.fgo.common.Constants;
import cn.growthgate.fgo.common.base.BaseVo;
import cn.growthgate.fgo.entity.ExceptionLog;
import cn.growthgate.fgo.service.IExceptionLogService;
import cn.growthgate.fgo.util.DateUtils;
import cn.growthgate.fgo.util.StringUtils;
import cn.growthgate.fgo.web.result.TableResult;

/**
 * <p>
 * 用户表 前端控制器
 * </p>
 *
 * @author GrowthGate
 * @since 2019-01-20
 */
@Controller
@RequestMapping("/exceptionLog")
@RequiresRoles(value = {Constants.ROLE_ADMIN}, logical = Logical.OR)
public class ExceptionLogController {

	@Autowired
	private IExceptionLogService exceptionLogService;
	
	@RequestMapping("/list")
	public String list() {
		return "exceptionLog/list";
	}
	
	@ResponseBody
	@RequestMapping("/tableData")
	public TableResult<BaseVo<ExceptionLog>> tableData(String addTime, Integer current, Integer size, String orderByField,
			Boolean isAsc) {
		Wrapper<ExceptionLog> wrapper = new EntityWrapper<>();
		
		if (StringUtils.isNotBlank(addTime)) {
			try {
				Date[] dates = DateUtils.split(addTime, DateUtils.DEFAULT_DATETIME);
				wrapper.between("addTime", dates[0], dates[1]);
			} catch (Exception e) {
				return new TableResult<BaseVo<ExceptionLog>>(HttpStatus.BAD_REQUEST.value(), "loginTime格式错误");
			}
		}
		
		Page<ExceptionLog> page = exceptionLogService.selectPage(current, size, orderByField, isAsc, wrapper);
		return new TableResult<BaseVo<ExceptionLog>>(exceptionLogService.transformVo(page));
	}

}
